package cn.ac.jaas.isc.vegetable.pbms.controller;

import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcBase;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.log.annotation.SysLog;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcBaseService;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.ac.jaas.isc.vegetable.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 基地表
 *
 * @author 张高昌
 * @date 2022-05-01 15:57:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/arcbase")
@Api(value = "arcbase", tags = "基地表管理")
public class ArcBaseController {

    private final ArcBaseService arcBaseService;

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param arcBase 基地表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('basic_arcbase_view')")
    public R getArcBasePage(Page page, ArcBase arcBase) {
        return R.ok(arcBaseService.page(page, Wrappers.query(arcBase)));
    }


    /**
     * 通过id查询基地表
     *
     * @param baseId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{baseId}")
    @PreAuthorize("@pms.hasPermission('basic_arcbase_view')")
    public R getById(@PathVariable("baseId") Integer baseId) {
        return R.ok(arcBaseService.getById(baseId));
    }

    /**
     * 新增基地表
     *
     * @param arcBase 基地表
     * @return R
     */
    @ApiOperation(value = "新增基地表", notes = "新增基地表")
    @SysLog("新增基地表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('basic_arcbase_add')")
    public R save(@RequestBody @Valid ArcBase arcBase) {
        return arcBaseService.saveBase(arcBase);
    }

    /**
     * 修改基地表
     *
     * @param arcBase 基地表
     * @return R
     */
    @ApiOperation(value = "修改基地表", notes = "修改基地表")
    @SysLog("修改基地表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('basic_arcbase_edit')")
    public R updateById(@RequestBody @Valid ArcBase arcBase) {
        if (arcBase.getBaseId() == null) {
            R.failed("基地id不能为空！");
        }
        return arcBaseService.updateBaseById(arcBase);
    }

    /**
     * 通过id删除基地表
     *
     * @param baseId id
     * @return R
     */
    @ApiOperation(value = "通过id删除基地表", notes = "通过id删除基地表")
    @SysLog("通过id删除基地表")
    @DeleteMapping("/{baseId}")
    @PreAuthorize("@pms.hasPermission('basic_arcbase_del')")
    public R removeById(@PathVariable Integer baseId) {
        return arcBaseService.removeBaseById(baseId);
    }


    /**
     * 导出excel 表格
     *
     * @param arcBase 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('basic_arcbase_export')")
    public List<ArcBase> export(ArcBase arcBase) {
        return arcBaseService.list(Wrappers.query(arcBase));
    }

    /**
     * 获取所有基地
     *
     * @return R
     */
    @ApiOperation(value = "获取所有基地", notes = "获取所有基地")
    @GetMapping("/getBaseAll")
    @PreAuthorize("@pms.hasPermission('basic_arcbase_view')")
    public R getBaseAll() {
        return R.ok(arcBaseService.list());
    }

}
